package com.situ.java.service;

import com.situ.java.domain.BorrowedRd;
import com.situ.java.domain.Status;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BorrowedListService implements Serializable{
    /** serialVersionUID*/  
    private static final long serialVersionUID = 1L;
    
    private BorrowedRd[] bords = new BorrowedRd[1024];// 借阅记录数组，暂时设置上限为1024本
    int totalBord = 0;


    //借书
    public void borrowedBook(int bookId, int personId) throws MyException {
        int flag = 0;
        for (int i = 0; i < PersonListService.totalPerson; i++) {       //借书卡ID是否存在
            if (personId == PersonListService.persons[i].getId()) {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            throw new MyException("该借书卡ID不存在，无法借阅");
        flag = 0;//初始化
        for (int i = 0; i < BookListService.totalBook; i++) {           //借阅的图书是否存在以及其状态
            if (bookId == BookListService.books[i].getId()) {
                if (Status.FREE == BookListService.books[i].getStatus()) {
                    flag = 1;
                    BookListService.books[i].setTime(BookListService.books[i].getTime() + 1);
                    BookListService.books[i].setStatus(Status.BUSY);
                } else {
                    flag = -1;  //所查询图书存在但是已经被借出
                }
                break;
            }
        }
        if (flag == 0)
            throw new MyException("该图书ID不存在，无法借阅");
        if (flag == -1)
            throw new MyException("该图书已被借阅，无法借阅");

        BorrowedRd brd = new BorrowedRd(bookId, personId);
        brd.setBorrowTime(getCurrentTime());
        bords[totalBord++] = brd;
    }

    // 获取当前时间
    private static Calendar getCurrentTime() {
        Calendar cal = Calendar.getInstance();
        return cal;
    }


    public void returnBook(int bookId) throws MyException {
        for (int i = 0; i < BookListService.totalBook; i++) {           //还书并更改其状态
            if (bookId == BookListService.books[i].getId()) {
                BookListService.books[i].setStatus(Status.FREE);
                break;
            }
        }
        for (int i = 0; i < totalBord; i++) {   //记录归还时间
            if (bookId == bords[i].getBookID()){
                bords[i].setReturnTime(getCurrentTime());
                break;
            }
        }
    }

    //获取某ID图书的借阅记录
    public List<BorrowedRd> getBookBords(int bookId){
        List<BorrowedRd> bords = new ArrayList<>();
        for (int i = 0; i < totalBord; i++) {
            if (bookId == this.bords[i].getBookID()){
                bords.add(this.bords[i]);
            }
        }
        return bords;
    }


    //获取某借书卡ID的借阅记录
    public List<BorrowedRd> getPersonBords(int personId){
        List<BorrowedRd> bords = new ArrayList<>();
        for (int i = 0; i < totalBord; i++) {
            if (personId == this.bords[i].getPersonID()){
                bords.add(this.bords[i]);
            }
        }
        return bords;
    }



}
